#include "net.h"
#include "gsm.h"
#include "string.h"
void Net_OpenPPD()
{
  AtCmdPostList(AT_CENG,1000,"",0);
  AtCmdPostList(AT_IPMUX,1000,"",0);
  AtCmdPostList(AT_CGATT,5000,"",0);  
  AtCmdPostList(AT_CSTT,1000,"",0);
  AtListStart();
}
void Net_OpenGPRS()
{
  AtCmdPostList(AT_CIICR,40000,"",0);
  AtCmdPostList(AT_CIFSR,5000,"",0);
  AtListStart();
}
void Net_CreatNewConnectForUser(unsigned char *ip,unsigned char *port)
{
  unsigned char atbufferlength=0;
  unsigned char atbuffer[50];
  memset(atbuffer,0,50);
  memcpy(&atbuffer[atbufferlength],"AT+CIPSTART=1,TCP,\"",19);
  atbufferlength+=19;
  memcpy(&atbuffer[atbufferlength],ip,strlen((const char*)ip));
  atbufferlength+=strlen((const char*)ip);
  memcpy(&atbuffer[atbufferlength],"\",",2);
  atbufferlength+=2;
  memcpy(&atbuffer[atbufferlength],port,strlen((const char*)port));
  atbufferlength+=strlen((const char*)port);
  memcpy(&atbuffer[atbufferlength],"\r\n",2);
  atbufferlength+=2;
  AtCmdPostList(AT_CREATSOCKET1,5000,atbuffer,atbufferlength);
  AtListStart();
}
void Net_CloseNewConnectForUser()
{
  unsigned char atbufferlength=0;
  unsigned char atbuffer[50];
  memset(atbuffer,0,50);
  memcpy(&atbuffer[atbufferlength],"AT+CIPCLOSE=1",13);
  atbufferlength+=13;
  memcpy(&atbuffer[atbufferlength],"\r\n",2);
  atbufferlength+=2;
	AtStatusChange(AT_NULL, 1000);
  AtCmdPostList(AT_CLOSESOCKET1,5000,atbuffer,atbufferlength);
  AtListStart();
}
void Net_CloseNewConnectForCloude()
{
  unsigned char atbufferlength=0;
  unsigned char atbuffer[50];
  memset(atbuffer,0,50);
  memcpy(&atbuffer[atbufferlength],"AT+CIPCLOSE=2",13);
  atbufferlength+=13;
  memcpy(&atbuffer[atbufferlength],"\r\n",2);
  atbufferlength+=2;
  AtCmdPostList(AT_CLOSESOCKET2,5000,atbuffer,atbufferlength);
  AtListStart();
}
void Net_CreatNewConnectForCloude(unsigned char *ip,unsigned char *port)
{
  unsigned char atbufferlength=0;
  unsigned char atbuffer[50];
  memset(atbuffer,0,50);
  memcpy(&atbuffer[atbufferlength],"AT+CIPSTART=2,UDP,\"",19);
  atbufferlength+=19;
  memcpy(&atbuffer[atbufferlength],ip,strlen((const char*)ip));
  atbufferlength+=strlen((const char*)ip);
  memcpy(&atbuffer[atbufferlength],"\",",2);
  atbufferlength+=2;
  memcpy(&atbuffer[atbufferlength],port,strlen((const char*)port));
  atbufferlength+=strlen((const char*)port);
  memcpy(&atbuffer[atbufferlength],"\r\n",2);
  atbufferlength+=2;
  AtCmdPostList(AT_CREATSOCKET2,5000,atbuffer,atbufferlength);
  AtListStart();
}
void Net_SendBufferTcp(unsigned char *buf,unsigned short len)
{
  unsigned char atbufferlength=0;
  unsigned char atbuffer[200];
	unsigned char CipSendbuf[200];
	unsigned char CipSendLen=0;
	memset(CipSendbuf,0,200);
  memset(atbuffer,0,200);
	memcpy(atbuffer,buf,len);
	atbufferlength=len;
	memcpy(CipSendbuf,"AT+CIPSEND=1,",strlen("AT+CIPSEND=1,"));
	CipSendLen=strlen("AT+CIPSEND=1,");
	if(len<=9)
	{
	CipSendbuf[CipSendLen++]=len+0x30;
	}
	else if(9<len&&len<=99)
	{
	CipSendbuf[CipSendLen++]=len/10+0x30;
	CipSendbuf[CipSendLen++]=len%10+0x30;
	}
	else if(99<len&&len<=999)
	{
	CipSendbuf[CipSendLen++]=len/100+0x30;
	CipSendbuf[CipSendLen++]=len%100/10+0x30;
	CipSendbuf[CipSendLen++]=len%10+0x30;
	}
	memcpy(&CipSendbuf[CipSendLen],"\r\n",2);
	CipSendLen+=2;
	if (USER_Socket.Connect==OK)
				{
	AtCmdPostList(AT_SENDSOCKET1,5000,CipSendbuf,CipSendLen);
  AtCmdPostList(AT_TCPSendData,20000,atbuffer,atbufferlength);
  AtListStart();
				}
}
void Net_SendBufferUdp(unsigned char *buf,unsigned short len)
{
  unsigned char atbufferlength=0;
  unsigned char atbuffer[200];
	unsigned char UdpSendbuf[200];
	unsigned char UdpSendLen=0;
	memset(UdpSendbuf,0,200);
  memset(atbuffer,0,200);
	memcpy(atbuffer,buf,len);
	atbufferlength=len;
  memcpy(UdpSendbuf,"AT+CIPSEND=2,",strlen("AT+CIPSEND=2,"));
	UdpSendLen=strlen("AT+CIPSEND=2,");
	if(len<=9)
	{
	UdpSendbuf[UdpSendLen++]=len+0x30;
	}
	else if(9<len&&len<=99)
	{
	UdpSendbuf[UdpSendLen++]=len/10+0x30;
	UdpSendbuf[UdpSendLen++]=len%10+0x30;
	}
	else if(99<len&&len<=999)
	{
	UdpSendbuf[UdpSendLen++]=len/100+0x30;
	UdpSendbuf[UdpSendLen++]=len%100/10+0x30;
	UdpSendbuf[UdpSendLen++]=len%10+0x30;
	}
	memcpy(&UdpSendbuf[UdpSendLen],"\r\n",2);
	UdpSendLen+=2;
	if(CLOUDE_Socket.Connect==OK)
				{
	AtCmdPostList(AT_SENDSOCKET2,5000,UdpSendbuf,UdpSendLen);
  AtCmdPostList(AT_UDPSendData,20000,atbuffer,atbufferlength);
  AtListStart();
				}
}
void Net_QueryCsq()
{
  AtCmdPostList(AT_CSQ,1000,"",0);
  AtListStart();
}

